<template>
  <div class="job-page">
    <!-- 导航栏 -->
    <NavBar title="兼职招聘">
      <template #right>
        <van-icon name="plus" size="20" color="white" @click="goToAdd" />
      </template>
    </NavBar>
    
    <!-- 内容区域 -->
    <div class="page-content">
      <!-- 搜索栏 -->
      <div class="search-bar">
        <van-search
          v-model="searchKeyword"
          placeholder="搜索兼职信息..."
          @search="handleSearch"
          @clear="handleClear"
        />
      </div>
      
      <!-- 列表 -->
      <div class="job-list">
        <div 
          v-for="job in jobList" 
          :key="job.id"
          class="job-item card"
          @click="goToDetail(job.id)"
        >
          <!-- 用户信息 -->
          <div class="job-header">
            <div class="user-info">
              <img :src="job.userAvatar || '/images/default-avatar.png'" alt="头像" class="avatar" />
              <div class="user-details">
                <div class="nickname">{{ job.userNickname }}</div>
                <div class="time">{{ formatTime(job.createTime) }}</div>
              </div>
            </div>
            <div class="job-status" v-if="job.isTop">
              <van-tag type="danger" size="mini">置顶</van-tag>
            </div>
          </div>
          
          <!-- 内容 -->
          <div class="job-content">
            <div class="title">{{ job.title }}</div>
            <div class="content">{{ job.content }}</div>
            
            <!-- 兼职信息 -->
            <div class="job-info">
              <div class="info-item">
                <span class="label">薪资:</span>
                <span class="value salary">{{ job.salary }}</span>
              </div>
              <div class="info-item">
                <span class="label">工作时间:</span>
                <span class="value">{{ job.workTime }}</span>
              </div>
              <div class="info-item">
                <span class="label">工作地点:</span>
                <span class="value">{{ job.location }}</span>
              </div>
              <div class="info-item">
                <span class="label">联系方式:</span>
                <span class="value contact">{{ job.contactWay }}</span>
              </div>
            </div>
            
            <!-- 图片 -->
            <div class="images" v-if="hasImages(job)">
              <img 
                v-if="job.pic1"
                :src="job.pic1" 
                alt="图片"
                class="content-image"
                @click.stop="previewImage(getImageList(job), 0)"
              />
              <img 
                v-if="job.pic2"
                :src="job.pic2" 
                alt="图片"
                class="content-image"
                @click.stop="previewImage(getImageList(job), 1)"
              />
              <img 
                v-if="job.pic3"
                :src="job.pic3" 
                alt="图片"
                class="content-image"
                @click.stop="previewImage(getImageList(job), 2)"
              />
            </div>
          </div>
          
          <!-- 底部操作 -->
          <div class="job-footer">
            <div class="stats">
              <span class="stat-item">
                <van-icon name="eye-o" size="14" />
                {{ job.viewCount }}
              </span>
              <span class="stat-item">
                <van-icon name="good-job-o" size="14" />
                {{ job.likeCount }}
              </span>
              <span class="stat-item">
                <van-icon name="chat-o" size="14" />
                {{ job.commentCount }}
              </span>
            </div>
          </div>
        </div>
      </div>
      
      <!-- 加载更多 -->
      <div class="load-more" v-if="hasMore">
        <van-button 
          :loading="loading" 
          @click="loadMore"
          type="default"
          size="small"
        >
          {{ loading ? '加载中...' : '加载更多' }}
        </van-button>
      </div>
      
      <!-- 空状态 -->
      <van-empty 
        v-if="!loading && jobList.length === 0"
        description="暂无兼职信息"
        image="search"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { showImagePreview, showToast } from 'vant'
import NavBar from '@/components/Common/NavBar.vue'
import { jobApi } from '@/api/job'
import type { JobPost } from '@/types'
import { formatTime } from '@/utils/format'

const router = useRouter()

// 响应式数据
const searchKeyword = ref('')
const jobList = ref<JobPost[]>([])
const loading = ref(false)
const hasMore = ref(true)
const currentPage = ref(1)
const pageSize = 10

// 加载列表数据
const loadList = async (isRefresh = false) => {
  if (loading.value) return
  
  loading.value = true
  
  try {
    const params = {
      page: isRefresh ? 1 : currentPage.value,
      size: pageSize,
      keyword: searchKeyword.value
    }
    
    const response = await jobApi.getList(params)
    const { list, total } = response.data
    
    if (isRefresh) {
      jobList.value = list
      currentPage.value = 1
    } else {
      jobList.value.push(...list)
    }
    
    hasMore.value = jobList.value.length < total
    currentPage.value++
  } catch (error) {
    showToast('加载失败')
    console.error('加载兼职列表失败:', error)
  } finally {
    loading.value = false
  }
}

// 搜索
const handleSearch = () => {
  currentPage.value = 1
  loadList(true)
}

// 清除搜索
const handleClear = () => {
  searchKeyword.value = ''
  currentPage.value = 1
  loadList(true)
}

// 加载更多
const loadMore = () => {
  loadList()
}

// 跳转到发布页面
const goToAdd = () => {
  router.push('/job/add')
}

// 跳转到详情页面
const goToDetail = (id: string) => {
  router.push(`/job/${id}`)
}

// 检查是否有图片
const hasImages = (item: any) => {
  return item.pic1 || item.pic2 || item.pic3
}

// 获取图片列表
const getImageList = (item: any) => {
  const images = []
  if (item.pic1) images.push(item.pic1)
  if (item.pic2) images.push(item.pic2)
  if (item.pic3) images.push(item.pic3)
  return images
}

// 预览图片
const previewImage = (images: string[], startPosition: number) => {
  showImagePreview({
    images,
    startPosition,
    closeable: true
  })
}

// 页面加载时获取数据
onMounted(() => {
  loadList(true)
})
</script>

<style scoped>
.job-page {
  min-height: 100vh;
  background-color: #f1f1f1;
}

.page-content {
  padding: 0 16px 16px;
}

.search-bar {
  background: white;
  border-radius: 8px;
  margin: 16px 0;
  overflow: hidden;
}

.job-list {
  margin-bottom: 16px;
}

.job-item {
  margin-bottom: 12px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.job-item:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.job-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
}

.avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  object-fit: cover;
}

.user-details {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.nickname {
  font-size: 14px;
  font-weight: 500;
  color: #333;
}

.time {
  font-size: 12px;
  color: #999;
}

.job-content {
  margin-bottom: 12px;
}

.title {
  font-size: 16px;
  font-weight: 500;
  color: #333;
  margin-bottom: 8px;
  line-height: 1.4;
}

.content {
  font-size: 14px;
  color: #666;
  line-height: 1.6;
  margin-bottom: 12px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.job-info {
  background: #f8f9fa;
  border-radius: 6px;
  padding: 12px;
  margin-bottom: 8px;
}

.info-item {
  display: flex;
  align-items: center;
  margin-bottom: 6px;
  font-size: 14px;
}

.info-item:last-child {
  margin-bottom: 0;
}

.label {
  color: #666;
  width: 80px;
  flex-shrink: 0;
}

.value {
  color: #333;
  flex: 1;
}

.salary {
  color: #e74c3c;
  font-weight: 500;
}

.contact {
  color: #636DEA;
}

.images {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  position: relative;
}

.content-image {
  width: 80px;
  height: 80px;
  border-radius: 6px;
  object-fit: cover;
  cursor: pointer;
}

.more-images {
  width: 80px;
  height: 80px;
  border-radius: 6px;
  background: rgba(0, 0, 0, 0.5);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  position: absolute;
  top: 0;
  right: 0;
}

.job-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-top: 8px;
  border-top: 1px solid #f0f0f0;
}

.stats {
  display: flex;
  gap: 16px;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  color: #999;
}

.load-more {
  text-align: center;
  padding: 16px 0;
}
</style>